package xm.com.handlertest;

import java.util.concurrent.LinkedTransferQueue;


public class TestThreadMsg1 {
    //阻塞式消息队列
    static LinkedTransferQueue mQueue = new LinkedTransferQueue();

    static class Thread1 extends Thread{
        @Override
        public void run() {
            super.run();
            for (;;) {
                String message = null;
                try {
                    message = (String) mQueue.take();//消息队列取出最新消息
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程:"+Thread.currentThread().getId()+"   收到:"+ message);
                System.out.println("");
                System.out.println("");
            }
        }
    }

    static class Thread2 extends Thread{
        @Override
        public void run() {
            super.run();
            for (;;) {
                try {
                    String message="来自线程"+Thread.currentThread().getId()+"的消息";
                    System.out.println("线程:"+Thread.currentThread().getId()+"   发送:"+message);
                    mQueue.put(message);//放到消息队列
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args){
        Thread1 t1 = new Thread1();
        t1.start();

        Thread2 t2 = new Thread2();
        t2.start();

    }
}
